package edu.scau.misp.backend_module.controller;

import edu.scau.misp.backend_module.core.domain.AjaxResult;
import edu.scau.misp.backend_module.core.rms_dto.ProductDTO;
import edu.scau.misp.backend_module.domain.Dish;
import edu.scau.misp.backend_module.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @GetMapping("/sn/{productSn}")
    public AjaxResult getProductBySn(@PathVariable("productSn")Long productSn){
        return AjaxResult.success(productService.getProductBySn(productSn));
    }

    @GetMapping("/listAll")
    public AjaxResult listAllProduct (){
        return AjaxResult.success(productService.listAllProduct());
    }

    @GetMapping("/list")
    public AjaxResult list(ProductDTO product) {
        List<ProductDTO> list = productService.selectProductList(product);
        return AjaxResult.success(list);
    }
    @PostMapping
    public AjaxResult add(@RequestBody ProductDTO product){
        int rows = productService.insertProduct(product);
        return rows > 0 ? AjaxResult.success("添加餐品组合成功") : AjaxResult.error("添加餐品组合失败");
    }
    @PutMapping
    public AjaxResult edit(@RequestBody ProductDTO product) {
        int rows = productService.updateProduct(product);
        return rows > 0 ? AjaxResult.success("修改餐品组合成功") : AjaxResult.error("修改餐品组合失败");
    }

    @DeleteMapping("/{productSn}")
    public AjaxResult remove(@PathVariable("productSn") Long productSn) {
        int rows = productService.deleteProductBySn(productSn);
        return rows > 0 ? AjaxResult.success("删除餐品组合成功") : AjaxResult.error("删除餐品组合失败");
    }

}
